# coding=utf-8
import requests

from Util.handle_init import handle_ini
from Base.base_request import request
from Util.handle_json import read_json, write_value
import json


def get_token():
    '''
    获取token参数

    '''
    url = handle_ini.get_value("url", "token")

    json_data = {}
    json_data["userName"] = handle_ini.get_value("userName", "token")
    json_data["password"] = handle_ini.get_value("password", "token")
    # header = {'content-type':'application/json;charset=utf-8'}
    res = request.send_post(url, json_data)
    res = json.loads(res)

    res_token = res.get('data').get('accessToken')

    return res_token


def write_token(res_token, token_key):
    '''
    写入token
    '''
    data1 = read_json("/Config/token.json")
    data1[token_key] = res_token
    write_value(data1)


def get_token_json():
    data = read_json("/Config/token.json")
    return data


if __name__ == "__main__":
    res_token = get_token()
    print(res_token)
    write_token("Bearer2 " + res_token, "Authorization")
